﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Routing;
using Microsoft.ApplicationInsights.Telemetry.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Gateway.UpdateService
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            SerializeSettings(GlobalConfiguration.Configuration);
            ServerAnalytics.Start("e59d36f8-d675-4720-bc26-a2b274dfc02d");
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            ServerAnalytics.BeginRequest();
            ServerAnalytics.CurrentRequest.LogEvent(Request.Url.AbsolutePath);
        }

        static void SerializeSettings(HttpConfiguration config)
        {
            var jsonSetting = new JsonSerializerSettings();
            jsonSetting.Converters.Add(new StringEnumConverter());
            config.Formatters.JsonFormatter.SerializerSettings = jsonSetting;
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }
    }
}
